package bank;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class BankUI extends JFrame{
	
	private AccountManager am;
	private JPanel contentPane;
	private JPanel panel;
	private JButton customerRegistButton;
	private JButton customerSearchButton;
	private JPanel panel_1;
	private JPanel panel_2;
	private JLabel lblNewLabel;
	private JTextField nameTextField;
	private JPanel panel_3;
	private JLabel lblNewLabel_1;
	private JTextField ageTextField;
	private JPanel panel_4;
	private JLabel lblNewLabel_2;
	private JTextField ssnTextField;
	private JPanel panel_5;
	private JLabel lblNewLabel_3;
	private JTextField addrTextField;
	private JPanel panel_6;
	private JPanel panel_7;
	private JLabel lblNewLabel_4;
	private JTextField accountTextField;
	private JPanel panel_8;
	private JPanel panel_9;
	private JLabel lblNewLabel_5;
	private JTextField balanceTextField;
	private JButton depositButton;
	private JButton withdrawButton;
	private JButton modifyCustomerButton;
	private JButton deleteCustomerButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BankUI frame = new BankUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BankUI() {
		am = new AccountManager();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 515, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		
		customerRegistButton = new JButton("고객등록");
		customerRegistButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent paramActionEvent) {
				CustomerRegistDialog customerRegistDialog = new CustomerRegistDialog();
				customerRegistDialog.setVisible(true);
				customerRegistDialog.addWindowListener(new WindowAdapter() {
					public void windowClosing(WindowEvent pwe) {
						AccountModel accountModel = customerRegistDialog.getAccountModel();
						if(accountModel == null)
							return;
						
						am.addAccount(accountModel);
					}
				});
			}
		});
		panel.add(customerRegistButton);
		
		customerSearchButton = new JButton("고객검색");
		customerSearchButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent paramActionEvent) {
				SearchDialog searchDialog = new SearchDialog();
				searchDialog.setVisible(true);
				searchDialog.addWindowListener(new WindowAdapter() {
					public void windowClosing(WindowEvent pwe) {
						System.out.println("searchAccount");
						AccountModel searchAccount = am.searchAccount(searchDialog.getSsn());
						System.out.println("searchAccount : " + searchAccount);
						
						if(searchAccount == null)
							return;
						
						setCustomerField(searchAccount);
						accountFieldDisable(false);
						
					}
					
					public void windowClosed(WindowEvent paramWindowEvent) {
						System.out.println("windowClosed : " + searchDialog.getSsn());
						AccountModel searchAccount = am.searchAccount(searchDialog.getSsn());
						System.out.println("windowClosed : " + searchAccount);
						
						if(searchAccount == null)
							return;
						
						setCustomerField(searchAccount);
						accountFieldDisable(false);
					}
				});
			}
		});
		panel.add(customerSearchButton);
		
		panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.WEST);
		panel_1.setLayout(new GridLayout(4, 1, 0, 0));
		
		panel_2 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		panel_1.add(panel_2);
		
		lblNewLabel = new JLabel("이름");
		panel_2.add(lblNewLabel);
		
		nameTextField = new JTextField();
		panel_2.add(nameTextField);
		nameTextField.setColumns(10);
		
		panel_3 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_3.getLayout();
		flowLayout_1.setAlignment(FlowLayout.RIGHT);
		panel_1.add(panel_3);
		
		lblNewLabel_1 = new JLabel("나이");
		panel_3.add(lblNewLabel_1);
		
		ageTextField = new JTextField();
		panel_3.add(ageTextField);
		ageTextField.setColumns(10);
		
		panel_4 = new JPanel();
		FlowLayout flowLayout_2 = (FlowLayout) panel_4.getLayout();
		flowLayout_2.setAlignment(FlowLayout.RIGHT);
		panel_1.add(panel_4);
		
		lblNewLabel_2 = new JLabel("주번");
		panel_4.add(lblNewLabel_2);
		
		ssnTextField = new JTextField();
		panel_4.add(ssnTextField);
		ssnTextField.setColumns(10);
		
		panel_5 = new JPanel();
		FlowLayout flowLayout_3 = (FlowLayout) panel_5.getLayout();
		flowLayout_3.setAlignment(FlowLayout.RIGHT);
		panel_1.add(panel_5);
		
		lblNewLabel_3 = new JLabel("주소");
		panel_5.add(lblNewLabel_3);
		
		addrTextField = new JTextField();
		panel_5.add(addrTextField);
		addrTextField.setColumns(10);
		
		panel_6 = new JPanel();
		contentPane.add(panel_6, BorderLayout.CENTER);
		panel_6.setLayout(new GridLayout(3, 1, 0, 0));
		
		panel_7 = new JPanel();
		panel_6.add(panel_7);
		
		lblNewLabel_4 = new JLabel("계좌");
		panel_7.add(lblNewLabel_4);
		
		accountTextField = new JTextField();
		panel_7.add(accountTextField);
		accountTextField.setColumns(10);
		
		panel_8 = new JPanel();
		panel_6.add(panel_8);
		
		lblNewLabel_5 = new JLabel("잔고");
		panel_8.add(lblNewLabel_5);
		
		balanceTextField = new JTextField();
		panel_8.add(balanceTextField);
		balanceTextField.setColumns(10);
		
		panel_9 = new JPanel();
		panel_6.add(panel_9);
		
		depositButton = new JButton("입금");
		panel_9.add(depositButton);
		
		withdrawButton = new JButton("출금");
		panel_9.add(withdrawButton);
		
		modifyCustomerButton = new JButton("고객수정");
		panel_9.add(modifyCustomerButton);
		
		deleteCustomerButton = new JButton("고객삭제");
		panel_9.add(deleteCustomerButton);
		
		accountFieldDisable(true);
	}
	
	//set
	
	public void setCustomerField(AccountModel am){
		nameTextField.setText(am.getName());
		ageTextField.setText(am.getAge());
		ssnTextField.setText(am.getSsn());
		addrTextField.setText(am.getAddress());
		accountTextField.setText(am.getAccountNumber());
		String balance = am.getBalance();
		if(balance == null)
			balance = "0";
		int parseInt = Integer.parseInt(balance);
		balanceTextField.setText(String.valueOf(parseInt));
	}
	
	public void accountFieldDisable(boolean b){
		if(b){
			depositButton.setEnabled(false);
			withdrawButton.setEnabled(false);
		}else{
			depositButton.setEnabled(true);
			withdrawButton.setEnabled(true);
		}
	}
	

}
